﻿/*
 * File:            Padding.cs
 * Last changed:    27-08-2011
 * Purpose:         Represents padding or margin information associated with a user interface (UI) element.
 * Author:          Lars Diederich
 * Copyright:       (C) L.Diederich 2011
 * Product:         UnifyGUI
 * 
 * Copyright (c) 2011 Lars Diederich
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace UnifyGUI
{
    /// <summary>
    /// Represents padding or margin information associated with a user interface (UI) element.
    /// </summary>
    public class Padding
    {
        private int _left;
        private int _top;
        private int _right;
        private int _bottom;

        private bool _all;

        /// <summary>
        /// Initializes a new instance of the Unify.Padding class using a separate padding size for each edge.
        /// </summary>
        /// <param name="left">The padding size, in pixels, for the left edge.</param>
        /// <param name="top">The padding size, in pixels, for the top edge.</param>
        /// <param name="right">The padding size, in pixels, for the right edge.</param>
        /// <param name="bottom">The padding size, in pixels, for the bottom edge.</param>
        public Padding(int left, int top, int right, int bottom)
        {
            this._top = top;
            this._left = left;
            this._right = right;
            this._bottom = bottom;

            this._all = ((this._top == this._left) && (this._top == this._right)) && (this._top == this._bottom);
        }

        /// <summary>
        /// Initializes a new instance of the Unify.Padding class using the supplied padding size for all edges.
        /// </summary>
        /// <param name="all">The number of pixels to be used for padding for all edges.</param>
        public Padding(int all)
        {
            this._all = true;
            this._top = all;
            this._left = all;
            this._right = all;
            this._bottom = all;
        }

        static Padding()
        {
            Empty = new Padding(0);
        }

        /// <summary>
        /// Provides a Unify.Padding object with no padding.
        /// </summary>
        public static readonly Padding Empty;

        /// <summary>
        /// Gets or sets the padding value for all the edges.
        /// </summary>
        /// <returns>The padding, in pixels, for all edges if the same; otherwise, -1.</returns>
        public int All
        {
            get
            {
                if (!this._all)
                {
                    return -1;
                }
                return this._top;
            }
            set
            {
                if (!this._all || (this._top != value))
                {
                    this._all = true;
                    this._top = value;
                    this._left = value;
                    this._right = value;
                    this._bottom = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the padding value for the top edge.
        /// </summary>
        /// <returns>The padding, in pixels, for the top edge.</returns>
        public int Top
        {
            get
            {
                return this._top;
            }
            set
            {
                if (this._all || (this._top != value))
                {
                    this._all = false;
                    this._top = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the padding value for the bottom edge.
        /// </summary>
        /// <returns>The padding, in pixels, for the bottom edge.</returns>
        public int Bottom
        {
            get
            {
                if (this._all)
                {
                    return this._top;
                }
                return this._bottom;
            }
            set
            {
                if (this._all || (this._bottom != value))
                {
                    this._all = false;
                    this._bottom = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the padding value for the left edge.
        /// </summary>
        /// <returns>The padding, in pixels, for the left edge.</returns>
        public int Left
        {
            get
            {
                if (this._all)
                {
                    return this._top;
                }
                return this._left;
            }
            set
            {
                if (this._all || (this._left != value))
                {
                    this._all = false;
                    this._left = value;
                }
            }
        }

        /// <summary>
        /// Gets or sets the padding value for the right edge.
        /// </summary>
        /// <returns>The padding, in pixels, for the right edge.</returns>
        public int Right
        {
            get
            {
                if (this._all)
                {
                    return this._top;
                }
                return this._right;
            }
            set
            {
                if (this._all || (this._right != value))
                {
                    this._all = false;
                    this._right = value;
                }
            }
        }

        /// <summary>
        /// Returns a string that represents the current Unify.Padding.
        /// </summary>
        /// <returns>A System.String that represents the current Unify.Padding.</returns>
        public override string ToString()
        {
            return ("{Left=" + this.Left.ToString(CultureInfo.CurrentCulture) + ",Top=" + this.Top.ToString(CultureInfo.CurrentCulture) + ",Right=" + this.Right.ToString(CultureInfo.CurrentCulture) + ",Bottom=" + this.Bottom.ToString(CultureInfo.CurrentCulture) + "}");
        }
    }
}
